package org.realogic.btp.web.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.realogic.btp.dhtmlx.define.DhtmlxGridModel;
import org.realogic.btp.dhtmlx.manager.DhtmlxGridManager;
import org.realogic.cfm.KAppContext;

public class DhtmlxGridItemTag extends TagSupport {

	private static final long serialVersionUID = -6369944507378856192L;
	
	private String name = null;
	private String field = null;

	public void setName(String name) {
		this.name = name;
	}

	public void setField(String field) {
		this.field = field;
	}

	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			DhtmlxGridManager tmpDhtmlxgridMgr = DhtmlxGridManager.getMe();
			DhtmlxGridModel tmpModel = tmpDhtmlxgridMgr.getDhtmlxGridModel(name);

			if (tmpModel != null) {
				if (tmpModel.isAuth() == false) {
					if ("headers".equals(field)) {
						out.print(tmpModel.getHeaders());
					} else if ("widths".equals(field)) {
						out.print(tmpModel.getWidths());
					} else if ("types".equals(field)) {
						out.print(tmpModel.getTypes());
					} else if ("aligns".equals(field)) {
						out.print(tmpModel.getAligns());
					} else if ("sorts".equals(field)) {
						out.print(tmpModel.getSorts());
					} else if ("colors".equals(field)) {
						out.print(tmpModel.getColors());
					} else if ("formats".equals(field)) {
						out.print(tmpModel.getFormats());
					} else if ("hiddens".equals(field)) {
						out.print(tmpModel.getHiddens());
					} else if ("attributes".equals(field)) {
						out.print(tmpModel.getAttributes());
					} else if ("footers".equals(field)) {
						out.print(tmpModel.getFooters());
					} else {
						throw new JspException(field + "不支持....");
					}
				} else {
					if ("headers".equals(field)) {
						out.print(tmpModel.getHeaders(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("widths".equals(field)) {
						out.print(tmpModel.getWidths(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("types".equals(field)) {
						out.print(tmpModel.getTypes(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("aligns".equals(field)) {
						out.print(tmpModel.getAligns(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("sorts".equals(field)) {
						out.print(tmpModel.getSorts(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("colors".equals(field)) {
						out.print(tmpModel.getColors(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("formats".equals(field)) {
						out.print(tmpModel.getFormats(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else if ("hiddens".equals(field)) {
						out.print(tmpModel.getHiddens(KAppContext.currentUser((HttpServletRequest) pageContext.getRequest())));
					} else {
						throw new JspException(field + "不支持....");
					}
				}
			} else {
				throw new JspException(name + "不存在....");
			}
		} catch (Throwable e) {
			throw new JspException(e);
		}
		return EVAL_PAGE;
	}
}